package soa.security.rs.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ldap.NamingException;
import org.springframework.stereotype.Service;
import soa.security.domain.Account;
import soa.security.exception.OrganizationException;
import soa.security.mgr.service.AccountMgrService;
import soa.security.rs.AccountService;
import soa.security.util.JsonUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author CreGu
 * @date 2016/3/29
 */
@Service
public class AccountRsServiceImpl implements AccountService{
    protected static final Logger log = LoggerFactory.getLogger(AccountRsServiceImpl.class);

    @Resource
    private AccountMgrService accountMgrService;

    @Override
    public String allAccounts(String organizationId) {
        try {
            List<Account> list = accountMgrService.allAccounts(organizationId);
            return JsonUtils.toJson(list);
        } catch (NamingException e) {
            throw new OrganizationException(OrganizationException.ERROR_LDAP, e);
        } catch (OrganizationException e) {
            throw e;
        } catch (Exception e) {
            if (log.isDebugEnabled())
                log.debug("其他错误", e);
            throw new OrganizationException(OrganizationException.ERROR_OTHER,
                    e);
        }
    }
}
